const mongoose = require('mongoose');
const jwt = require('jsonwebtoken');
const { collectionModel } = require('./../models/collectionModel');

// 测试完毕
async function demo(req,res){
    let result=await mongoose.model(collectionModel).find({studentId:req.params.id}).populate({
        path:"exerciseId",
    }).populate({
        path:"studentId",
    })
    // console.log(result)
    if (result.length > 0) {
        res.send({
            code: 1,
            message: '获取成功',
            result:result
        })

    } else {
        res.send({
            code: 0,
            message: '获取失败',
        })
    }
}

async function del(req,res){
    // console.log(req.body)
    let result=await mongoose.model('collectionModel').deleteOne({_id:req.body.id})
    if (result.deletedCount != 0) {
        res.send({
            code: 1,
            message: '删除成功',
        })

    } else {
        res.send({
            code: 0,
            message: '删除失败',
        })
    }
}

async function add(req,res){
    let result=await mongoose.model('collectionModel').find({...req.body});
    if (result.length>0) {
        res.send({
            message: '已添加到收藏',
            code: 0
        });
    } else {
       await mongoose.model('collectionModel').create({...req.body})
        res.send({
            message: '添加成功',
            code: 1
        })
    }
}
module.exports={demo,del,add}